<template>
  <view v-if="!isLoading" class="container">
    <!-- 经营概况 -->
    <view class="my-asset">
      <view class="asset-left flex-box dis-flex flex-x-center">
        <view class="asset-left-item" @click="onTargetSale">
          <view class="item-value dis-flex flex-x-center">
            <text>{{ storeData.overview.orderTotalPrice.tday }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>销售额</text>
          </view>
        </view>
        <view class="asset-left-item" @click="onTargetOrder">
          <view class="item-value dis-flex flex-x-center">
            <text>{{ storeData.overview.orderTotal.tday }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>支付订单数</text>
          </view>
        </view>
        <view class="asset-left-item" @click="onTargetUser">
          <view class="item-value dis-flex flex-x-center">
            <text>{{ storeData.statistics.userTotal }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>会员数</text>
          </view>
        </view>
        <view class="asset-left-item" @click="onTargetUser(1)">
          <view class="item-value dis-flex flex-x-center">
            <text>{{ storeData.statistics.loseUserTotal }}</text>
          </view>
          <view class="item-name dis-flex flex-x-center">
            <text>会员流失量</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 订单操作 -->
    <view class="order-navbar">
      <view class="order-navbar-item" v-for="(item, index) in orderNavbar" :key="index" @click="onTargetOrder(item)">
        <view class="item-icon">
          <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
        </view>
        <view class="item-name">{{ item.name }}</view>
        <text class="order-badge" v-if="item.count && item.count > 0">{{ item.count }}</text>
      </view>
    </view>

	<!-- 商品管理 -->
	<view class="my-service">
      <!-- <view class="service-title">我的服务</view> -->
      <view class="service-content clearfix">
        <block v-for="(item, index) in goodsService" :key="index">
          <view v-if="item.type == 'link'" class="service-item" @click="handleService(item)">
            <view class="item-icon">
              <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
            </view>
            <view class="item-name">{{ item.name }}</view>
          </view>
          <view v-if="item.type == 'button' && $platform == 'MP-WEIXIN'" class="service-item">
            <button class="btn-normal" :open-type="item.openType">
              <view class="item-icon">
                <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
              </view>
              <view class="item-name">{{ item.name }}</view>
            </button>
          </view>
        </block>
      </view>
    </view>
	
    <!-- 我的服务 -->
    <view class="my-service">
      <!-- <view class="service-title">我的服务</view> -->
      <view class="service-content clearfix">
        <block v-for="(item, index) in service" :key="index">
          <view v-if="item.type == 'link'" class="service-item" @click="handleService(item)">
            <view class="item-icon">
              <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
            </view>
            <view class="item-name">{{ item.name }}</view>
          </view>
          <view v-if="item.type == 'button' && $platform == 'MP-WEIXIN'" class="service-item">
            <button class="btn-normal" :open-type="item.openType">
              <view class="item-icon">
                <text class="iconfont" :class="[`icon-${item.icon}`]"></text>
              </view>
              <view class="item-name">{{ item.name }}</view>
            </button>
          </view>
        </block>
      </view>
    </view>

    <!-- 退出登录 -->
    <view v-if="isLogin" class="my-logout">
      <view class="logout-btn" @click="handleLogout()">
        <text>退出登录</text>
      </view>
    </view>
	
	<u-mask :show="noticeShow" @click="noticeShow = false">
		<view class="warp">
			<view class="rect" @tap.stop>
				<view class="notice-title">平台通知</view>
				<view class="notice-content">{{ noticeContent }}</view>
			</view>
		</view>
	</u-mask>
	
  </view>
</template>

<script>
  import shop from '@/shop'
  import { setCartTabBadge } from '@/utils-shop/app'
  import SettingKeyEnum from '@/common/enum/setting/Key'
  import SettingModel from '@/common/model/Setting'
  import * as UserApi from '@/apishop/user'
  import * as StoreApi from '@/apishop/store'
  import * as OrderApi from '@/apishop/order'
  import { checkLogin } from '@/utils-shop/app'
  import * as SettingApi from '@/apishop/setting'

  // 订单操作
  const orderNavbar = [
    { id: 'all', name: '全部订单', icon: 'qpdingdan' },
    { id: 'pay', name: '待支付', icon: 'daifukuan', count: 0 },
    // { id: 'delivery', name: '待发货', icon: 'daifahuo', count: 0 },
    { id: 'receipt', name: '待收货', icon: 'daishouhuo', count: 0 },
    { id: 'complete', name: '已完成', icon: 'daifahuo', count: 0 },
  ]

  const goodsService = [
    { id: 'goods-add', name: '添加商品', icon: 'bangzhu', type: 'link', url: 'pages/shop/goods/add' },
    { id: 'goods', name: '商品管理', icon: 'shouhuodizhi', type: 'link', url: 'pages/shop/goods/list' },
    { id: 'goods-category', name: '商品分类', icon: 'shouhuodizhi', type: 'link', url: 'pages/shop/category/list' },
    { id: 'checkout', name: '代客下单', icon: 'shouhou', type: 'link', url: 'pages/shop/category/index' },
  ]
  /**
   * 我的服务
   * id: 标识; name: 标题名称; icon: 图标; type 类型(link和button); url: 跳转的链接
   */
  const service = [
    { id: 'appointment', name: '预约管理', icon: 'bangzhu', type: 'link', url: 'pages/shop/appointment/list' },
    { id: 'user', name: '客户管理', icon: 'lingquan', type: 'link', url: 'pages/shop/user/list' },
	// todo 
    { id: 'statistics', name: '销售统计', icon: 'jifen', type: 'link', url: 'pages/shop/statistics/index' },
    // { id: 'finance', name: '对账单', icon: 'jifen', type: 'link', url: 'pages/shop/finance/index' },
    { id: 'withdraw', name: '提现管理', icon: 'jifen', type: 'link', url: 'pages/shop/withdraw/list' },
    { id: 'staff', name: '员工管理', icon: 'bangzhu', type: 'link', url: 'pages/shop/staff/list' },
    { id: 'qrcode', name: '店铺二维码', icon: 'shouhou', type: 'button', url: 'pages/shop/store/qrcode' },
    { id: 'set-withdraw', name: '提现帐户', icon: 'shouhou', type: 'link', url: 'pages/shop/setting/withdraw' },
    { id: 'set-grade', name: '会员等级', icon: 'shouhou', type: 'link', url: 'pages/shop/grade/index' },
    { id: 'set-basic', name: '店铺设置', icon: 'shouhou', type: 'link', url: 'pages/shop/setting/index' },
    { id: 'set-notice', name: '通知设置', icon: 'shouhou', type: 'link', url: 'pages/shop/setting/notice' },
    { id: 'set-images', name: '广告设置', icon: 'shouhou', type: 'link', url: 'pages/shop/setting/images' },
    { id: 'set-points', name: '积分设置', icon: 'shouhou', type: 'link', url: 'pages/shop/setting/points' },
    { id: 'renew', name: '修改密码', icon: 'shouhou', type: 'link', url: 'pages/shop/renew/index' },
  ]

  export default {
    data() {
      return {
		noticeShow: false,
		noticeContent: '',
        // 枚举类
        SettingKeyEnum,
        // 当前运行的终端 (此处并不冗余,因为微信小程序端view层无法直接读取$platform)
        $platform: this.$platform,
        // 正在加载
        isLoading: true,
        // 是否已登录
        isLogin: false,
        // 系统设置
        setting: {},
        // 当前用户信息
        userInfo: {},
		// 当前店铺信息
        storeInfo: {},
        // 账户资产
        storeData: {},
		// 商品服务
		goodsService,
        // 我的服务
        service,
        // 订单操作
        orderNavbar,
        // 当前用户待处理的订单数量
        todoCounts: { payment: 0, deliver: 0, received: 0 }
      }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow(options) {
      this.onRefreshPage()
    },

    methods: {

      // 刷新页面
      onRefreshPage() {
        // 更新购物车角标
        // setCartTabBadge()
        // 判断是否已登录
        this.isLogin = checkLogin()
        // 获取页面数据
        this.getPageData()
      },

      // 获取页面数据
      getPageData(callback) {
        const app = this
        app.isLoading = true
        Promise.all([app.getSetting(), app.getUserInfo(), app.getStoreInfo(), app.getStoreData()])
          .then(result => {
			  console.log(result)
            app.isLoading = false
            // 初始化我的服务数据
            app.initService()
            // 初始化订单操作数据
            app.initOrderTabbar()
            // 执行回调函数
            callback && callback()
          })
          .catch(err => {
            console.log('catch', err)
          })
      },

      // 初始化我的服务数据
      initService() {
        const app = this
        const newService = []
        service.forEach(item => {
          newService.push(item)
        })
        app.service = newService
      },

      // 初始化订单操作数据
      initOrderTabbar() {
        const app = this
        const newOrderNavbar = []
        orderNavbar.forEach(item => {
          if (item.hasOwnProperty('count')) {
            item.count = app.todoCounts[item.id]
          }
          newOrderNavbar.push(item)
        })
        app.orderNavbar = newOrderNavbar
      },

      // 获取商城设置
      getSetting() {
        const app = this
        return new Promise((resolve, reject) => {
          SettingModel.data().then(setting => {
            app.setting = setting
            resolve(setting)
          }).catch(reject)
        })
      },

      // 获取当前用户信息
      getUserInfo() {
        const app = this
        return new Promise((resolve, reject) => {
          // !app.isLogin ? resolve(null) : UserApi.info()
          UserApi.info()
            .then(result => {
              app.userInfo = result.data
              resolve(app.userInfo)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取店铺信息
      getStoreInfo() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : StoreApi.info()
            .then(result => {
              app.storeInfo = result.data.data
              resolve(app.storeInfo)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取当前店铺的数据
      getStoreData() {
        const app = this
        return new Promise((resolve, reject) => {
          !app.isLogin ? resolve(null) : StoreApi.data()
            .then(result => {
              app.storeData = result.data.data
              resolve(app.storeData)
            })
            .catch(err => {
              if (err.result && err.result.status == 401) {
                app.isLogin = false
                resolve(null)
              } else {
                reject(err)
              }
            })
        })
      },

      // 获取当前店铺的数据
      getNotice() {
        const app = this
        SettingApi.getNoticePlatform()
          .then(result => {
            app.noticeShow = result.data.is_use == 1 ? true : false;
            app.noticeContent = result.data.content
          })
      },
	  
      // 退出登录
      handleLogout() {
        const app = this
        uni.showModal({
          title: '友情提示',
          content: '您确定要退出登录吗?',
          success(res) {
            if (res.confirm) {
              shop.dispatch('Logout', {})
                .then(result => {
					app.$navTo('pages/user/index')
				})
            }
          }
        })
      },

      // 跳转到订单页
      onTargetOrder(item) {
        this.$navTo('pages/shop/order/index', { dataType: item.id })
      },

      // 跳转到服务页面
      handleService({ url }) {
        this.$navTo(url)
      },

      // 跳转到订单页
      onTargetSale(item) {
        this.$navTo('pages/shop/statistics/index')
      },
	  
	  // 跳转到订单页
	  onTargetUser(lose) {
	    this.$navTo('pages/shop/user/list', {lose})
	  },
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
      // 获取首页数据
      this.getPageData(() => {
        uni.stopPullDownRefresh()
      })
    },


  }
</script>

<style lang="scss" scoped>
  // 页面头部
  .main-header {
    background-color: #fff;
    // background-image: url('/static/background/user-header.png');
    position: relative;
    width: 100%;
    height: 280rpx;
    background-size: 100% 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    // padding-top: 40rpx;
    padding-left: 30rpx;

    .bg-image {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 0;
    }

    .user-info {
      display: flex;
      height: 100rpx;
      z-index: 1;

      .user-avatar .image {
        display: block;
        width: 100rpx;
        height: 100rpx;
        border-radius: 50%;
      }

      .user-content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: 30rpx;
        color: #c59a46;

        .nick-name {
          font-size: 32rpx;
          font-weight: bold;
        }

        .mobile {
          margin-top: 15rpx;
          font-size: 26rpx;
        }

        .user-grade {
          display: flex;
          align-items: center;
          background: #3c3c3c;
          margin-top: 12rpx;
          border-radius: 10rpx;
          padding: 4rpx 12rpx;

          .user-grade_icon .image {
            display: block;
            width: 32rpx;
            height: 32rpx;
          }

          .user-grade_name {
            margin-left: 5rpx;
            font-size: 24rpx;
            color: #EEE0C3;
          }

        }

        .login-tips {
          margin-top: 12rpx;
          font-size: 28rpx;
        }

      }
    }
  }

  // 我的钱包
  .my-asset {
    display: flex;
    background: #fff;
    padding: 40rpx 0;

    .asset-right {
      width: 200rpx;
      border-left: 1rpx solid #eee;
    }

    .asset-right-item {
      text-align: center;
      color: #545454;

      .item-icon {
        font-size: 40rpx;
      }

      .item-name {
        margin-top: 10rpx;
      }

      .item-name text {
        font-size: 25rpx;
      }

    }

    .asset-left-item {
      text-align: center;
      color: #666;
      padding: 0 42rpx;

      .item-value {
        font-size: 32rpx;
        color: red;
      }

      .item-name {
        margin-top: 6rpx;
      }

      .item-name {
        font-size: 25rpx;
      }
    }

  }

  // 订单操作
  .order-navbar {
    display: flex;
    margin: 20rpx auto 20rpx auto;
    padding: 20rpx 0;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    font-size: 30rpx;
    border-radius: 5rpx;
    background: #fff;

    &-item {
      position: relative;
      width: 25%;

      .item-icon {
        text-align: center;
        margin: 0 auto;
        padding: 10rpx 0;
        color: #545454;
        font-size: 40rpx;
      }

      .item-name {
        font-size: 24rpx;
        color: #545454;
        text-align: center;
        margin-right: 10rpx;
      }

      .order-badge {
        position: absolute;
        top: 0;
        right: 55rpx;
        font-size: 22rpx;
        background: #fa2209;
        text-align: center;
        line-height: 28rpx;
        color: #fff;
        border-radius: 100%;
        min-height: 30rpx;
        min-width: 30rpx;
        padding: 1rpx;
      }
    }
  }

  // 我的服务
  .my-service {
    margin: 22rpx auto 22rpx auto;
    padding: 20rpx 0;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 5rpx;
    background: #fff;

    .service-title {
      padding-left: 20rpx;
      margin-bottom: 30rpx;
      font-size: 28rpx;
    }

    .service-content {

      // margin-bottom: -30rpx;
      .service-item {
        position: relative;
        width: 25%;
        float: left;
        margin-bottom: 30rpx;

        .item-icon {
          text-align: center;
          margin: 0 auto;
          padding: 10rpx 0;
          color: #ff3800;
          font-size: 40rpx;
        }

        .item-name {
          font-size: 24rpx;
          color: #545454;
          text-align: center;
          margin-right: 10rpx;
        }

      }
    }
  }

  // 退出登录
  .my-logout {
    display: flex;
    justify-content: center;
    margin-top: 30rpx;

    .logout-btn {
      margin: 0 auto;
      display: inline-block;
      font-size: 26rpx;
      color: #616161;
      border-radius: 20rpx;
      border: 1px solid #dcdcdc;
      padding: 14rpx 150rpx;
    }
  }
  
    // 通知遮罩层
    .warp {
		display: flex;
  		align-items: center;
  		justify-content: center;
  		height: 100%;
  	}
  	.rect {
  		width: 60%;
  		min-height: 200upx;
  		background-color: #fff;
		border-radius: 10upx;
  	}
	
	.notice-title{
		text-align: center;
		margin: 10upx 0;
	}
	.notice-content{
		margin: 20upx;
	}
</style>
